package net.zjitc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.mapper.UserMapper;
import net.zjitc.pojo.*;
import net.zjitc.service.CartService;
import net.zjitc.service.OrderService;
import net.zjitc.service.UserService;
import net.zjitc.vo.OrderConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @AUTHOR zhj
 * @DATE 2019/3/31 13:31
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    UserMapper userMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    CartService cartService;


    @Override
    public List<Orders> findAll() {

        List<Orders> ordersList = orderMapper.findAllOrder();
        setOrdersUser(ordersList);
        return ordersList;
    }


    @Override
    public Orders findByOid(String id) {
        return orderMapper.findById(id);
    }

    /**
     * 多条件查询订单信息
     * @param vo
     * @return
     */
    @Override
    public List<Orders> findByCondition(OrderConditionVo vo) {
        List<Orders> ordersList = orderMapper.findByCondition(vo);
        setOrdersUser(ordersList);//用户信息
        return ordersList;

    }

    /**
     * 订单详情的处理
     * @param id
     * @return
     */
    @Override
    public Orders findOrderDeatils(String id) {
        Orders orders = orderMapper.findById(id);
        //查询用户信息
        User user = userMapper.selectById(orders.getUser_id());
        orders.setUser(user);

        //查询订单子项(orderitem中是复合主键)
        findItemsAndProductDetailByOrder(orders);
        return orders;
    }

    /**
     * 查询当前用户的订单列表
     * @param id
     * @return
     */
    @Override
    public List<Orders> findOrdersByUid(Integer id) {
        List<Orders> ordersList = orderMapper.findOrdersByUid(id);
        return ordersList;
    }

    /**
     * 创建订单
     * @param orders
     * @param user
     * @return
     */
    @Override
    public int createOrder(Orders orders, User user) {
        /**
         * 1. 通过购物车项查询购物车条目
         * 2. 设置订单order信息 state=0;表示未付款
         * 3. 根据购物车条目，设置订单条目信息，并将订单条目放到订单的订单条目集合中（准备总价格）
         * 4. 设置订单的总的价格
         * 5  将订单添加到数据库中---》service
         * 6. 删除购物车条目---》service
         * 7. 订单放在request域中，并转发到页面
         * */
        List<Cart> cartList = cartService.findCartsByUid(user.getId());
        //设置订单信息,其它信息已经由页面传过来了
        orders.setId(UUID.randomUUID().toString());
        orders.setPaystate(0);
        orders.setUser_id(user.getId());
        //根据购物车信息生成订单条目信息
        List<Orderitem> orderitemList = new ArrayList<>();
        for (Cart cart:cartList){
            Orderitem orderitem = new Orderitem();
            orderitem.setBuynum(cart.getQuantity());
            orderitem.setOid(orders.getId());
            orderitem.setPid(cart.getPid());
            orderitemList.add(orderitem);
        }

        //插入订单
        int resultCount =  orderMapper.createOrders(orders);
        if (resultCount == 0){
            return 0;
        }

        //插入订单条目
        resultCount = orderMapper.createOrderItems(orderitemList);
        if (resultCount==0){
            return 0;
        }

        //删除购物车内容
        resultCount = cartService.deleteCartByUid(user.getId());
        return resultCount;
    }

    @Override
    public int updatePayState(String out_trade_no,Integer state) {
        return orderMapper.updatePayState(out_trade_no,state);
    }

    /**
     * 设置订单所属的用户信息
     * @param ordersList
     */
    private void setOrdersUser(List<Orders> ordersList) {
        for (int i = 0;i<ordersList.size();i++){
            Orders orders = ordersList.get(i);
            User user = userMapper.selectById(orders.getUser_id());
            orders.setUser(user);
        }
    }


    private void findItemsAndProductDetailByOrder(Orders orders){
        List<Orderitem> orderitemList = orderMapper.findByOid(orders.getId());
        for (Orderitem orderitem:orderitemList){
            Product product = productMapper.findById(orderitem.getPid());
            orderitem.setProduct(product);
        }
        orders.setOrderitemList(orderitemList);
    }

}
